package com.mobile.challenge.service.impl;

import com.mobile.challenge.dao.PrizeMapper;
import com.mobile.challenge.dao.PrizeRecordMapper;
import com.mobile.challenge.model.Prize;
import com.mobile.challenge.model.PrizeRecord;
import com.mobile.challenge.service.PrizeRecordService;
import com.mobile.challenge.core.AbstractService;
import com.mobile.challenge.service.RuleTitleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


/**
 * Created by CodeGenerator on 2018/06/04.
 */
@Service
@Transactional
public class PrizeRecordServiceImpl extends AbstractService<PrizeRecord> implements PrizeRecordService {
    @Resource
    private PrizeRecordMapper tPrizeRecordMapper;

    @Resource
    private PrizeMapper tPrizeMapper;

    @Resource
    private RuleTitleService ruleTitleService;

    @Override
    public Map findByPhoneNo(String phoneNo) {
        int times = tPrizeMapper.selectByPrimaryKey(1).getPrizeTimesEveryone();
        List<PrizeRecord> prizeList=tPrizeRecordMapper.queryCountByPhoneNo(phoneNo);
        Map resultData = new HashMap();
        resultData.put("count",prizeList.size());
        //达到中奖上限
        if(prizeList.size()==times){
            resultData.put("code",0);
            resultData.put("message","No chance of won the prize");
        }
        else{
            resultData.put("code",1);
            resultData.put("message","Have chance of won the prize");
        }

        return resultData;
    }

    @Override
    public Map myPrize(String phoneNo) {
        //该用户获奖几次
        List<PrizeRecord> myPrizeList=tPrizeRecordMapper.queryCountByPhoneNo(phoneNo);
        //每个奖品多少M流量
        Prize prize = tPrizeMapper.selectByPrimaryKey(1);
        Map resultData = new HashMap();
        resultData.put("count",myPrizeList.size() * prize.getPrizeValue());

        return resultData;
    }

    @Override
    public Map add(PrizeRecord prizeRecord) {
        Map result = new HashMap();
        Map rs = ruleTitleService.isOverdue();
        //剩余多少奖品
        int count = tPrizeMapper.selectByPrimaryKey(1).getPrizeTotal();
        rs.put("count",count);
        /*
            活动未开始或已过期
         */
        if(!(boolean)rs.get("code")){

            result.put("code",false);
            result.put("msg",rs.get("msg"));
        }
        /*
            活动中
         */
        else{
            /*
                奖品已经发光
             */
            if(count == 0){
                result.put("code",false);
                result.put("msg","奖品已发光");
            }
            else{
                int myPrizeCount = tPrizeRecordMapper.queryCountByPhoneNo(prizeRecord.getPhoneNo()).size();

                int times = tPrizeMapper.selectByPrimaryKey(1).getPrizeTimesEveryone();
                if(myPrizeCount == times){
                    result.put("code",false);
                    result.put("msg","此用户已达到本次活动的获奖上限");
                }
                else{
                    result.put("code",true);
                    //增加一条中奖记录
                    tPrizeRecordMapper.insertSelective(prizeRecord);
                    //总奖数减1
                    tPrizeMapper.updatePrizeTotal();
                }
            }
        }

        return result;
    }
}
